<?php
/**
 *
 * Base (前台公共模块)
 *
 */
class PublicAction extends CommonAction {
    protected $Config;
    protected $sysConfig;
    protected $categorys;
    protected $module;
    protected $moduleid;
    protected $mod;
    protected $db;
    protected $Type;
    protected $forward;
    protected $user_menu;
    protected $Lang;
    protected $member_config;


    public function _initialize()
    {
        parent::_initialize();
        $this->sysConfig = F('sys.config');
        $this->module    = F('Module');
        $this->Type      = F('Type');
        $this->mod       = F('Mod');

        $this->Lang = F('Lang');
        $this->assign('Lang',$this->Lang);
        if (isset($_GET['l'])) {
            $lang = $_GET['l'];
        } else {
            $lang = $this->sysConfig['DEFAULT_LANG'];
        }
        define('LANG_NAME', $lang);
        define('LANG_ID', $this->Lang[$lang]['id']);
        $this->categorys = F('Category_'.$lang);
        $this->Config = F('Config_'.$lang);
        $this->assign('l',$lang);
        $this->assign('langid',LANG_ID);
        $T = F('config_'.$lang,'', './Themes/Home/'.$this->sysConfig['DEFAULT_THEME'].'/');
        C('TMPL_CACHFILE_SUFFIX',$lang.C('TMPL_CACHFILE_SUFFIX'));
        cookie('think_language',$lang);
        if ($lang=='zh-cn') {
            cookie('think_template',C('DEFAULT_THEME'));
        }


        $_GET['id'] = !empty($_GET['id'])?$_GET['id']:0;
        $c_atid = !empty($_GET['catid']) ? $_GET['catid'] : $_GET['id'];
        $c_atid = intval($c_atid);

        if(empty($c_atid)){
            $Cat = F('Cat_'.$lang);
            $c_atid = $Cat[I('get.catdir')];
        }

        if(array_key_exists($c_atid,$this->categorys)){
            $parencats = explode(',',$this->categorys[$c_atid]['arrparentid']);
            $max_parent_catid = empty($parencats[1]) ? $c_atid : $parencats[1];
            $this->assign('max_parent_catid',$max_parent_catid);
            $this->assign('max_parent_catname',$this->categorys[$max_parent_catid]['catname']);
        }

        $this->assign('T',$T);
        $this->assign($this->Config);
        $this->assign('Type',$this->Type);
        $this->assign('Module',$this->module);
        $this->assign('Cats',$this->categorys);
        import("@.ORG.Form");
        $this->assign('form',new Form());


        C('PAGE_LISTROWS',$this->sysConfig['PAGE_LISTROWS']);
        C('URL_M',$this->sysConfig['URL_MODEL']);
        C('URL_M_PATHINFO_DEPR',$this->sysConfig['URL_PATHINFO_DEPR']);
        C('URL_M_HTML_SUFFIX',$this->sysConfig['URL_HTML_SUFFIX']);
        C('URL_LANG',$this->sysConfig['DEFAULT_LANG']);
        C('DEFAULT_THEME_NAME',$this->sysConfig['DEFAULT_THEME']);

        if ($_SERVER['HTTP_X_REWRITE_URL']) {
            $current = $_SERVER['HTTP_X_REWRITE_URL'];
        } else {
            $current = $_SERVER['REQUEST_URI'];
        }

        $this->wapconfig = array();
        if (F('Wapconfig_'.LANG_NAME)) {
            $this->wapconfig = F('Wapconfig_'.LANG_NAME);
        } else {
            $data = M('WapConfig')->where('lang='.LANG_ID)->select();
            foreach ($data as $key=>$val) {
                $this->wapconfig[$val['varname']] = $val['value'];
            }
            F('Wapconfig_'.LANG_NAME, $this->wapconfig);
        }

        $this->assign($this->wapconfig);
        $this->assign('current',$current);

        //获取根域名
        $_SESSION['rootdomain'] = GetUrlToDomain($_SERVER["HTTP_HOST"]);

        //读取碎片
        $block = $this->getBlock();
        $this->assign('block', $block);
    }

    function getBlock()
    {
        $path = './Themes/Wap/'.C('DEFAULT_THEME').'/Block';
        $dir_handle = opendir($path);                   //打开文件系统所要处理的目录
        $block = array();
        while($file = readdir($dir_handle)) {           //遍历目录下的所有对象
            if($file!="." && $file!="..") {             //去掉目录下当前目录和上一级目录
                $blockname = str_replace('.html', '', $file);
                $filename = $path."/".$file;            //将目录名和当前目录下的文件名相连
                if (is_file($filename)) {               //如果遍历的是文件
                    $block[$blockname] = file_get_contents($filename);
                }
            }
        }

        return $block;
    }

    public function hits()
    {
        $module    = $module ? $module : MODULE_NAME;
        $id        = $id ? $id : intval($_REQUEST['id']);
        $this->db = M($module);
        $this->db->where("id=".$id)->setInc('hits');

        if($module=='Download'){
            $r = $this->db->find($id);
            echo '$("#hits").html('.$r['hits'].');$("#downs").html('.$r['downs'].');';
        }else{
            $hits = $this->db->where("id=".$id)->getField('hits');
            echo '$("#hits").html('.$hits.');';
        }
        exit;
    }

    public function verify()
    {
        header('Content-type: image/jpeg');
        $type = isset($_GET['type'])?$_GET['type']:'jpeg';
        import("@.ORG.Image");
        Image::buildImageVerify(4,1,$type);
    }
}